package com.yiqi.liebang.feature.news.view;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseFragment;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.liebang.R;
import com.yiqi.liebang.entity.bo.MessageBo;
import com.yiqi.liebang.entity.bo.VisitorBo;
import com.yiqi.liebang.entity.enums.Certificate;
import com.yiqi.liebang.feature.home.view.AskInfoActivity;
import com.yiqi.liebang.feature.login.view.LoginActivity;
import com.yiqi.liebang.feature.mine.view.CertificationFailActivity;
import com.yiqi.liebang.feature.mine.view.CertificationSuccessActivity;
import com.yiqi.liebang.feature.mine.view.UserInfoActivity;
import com.yiqi.liebang.feature.news.contract.MessageContract;
import com.yiqi.liebang.feature.news.dependencies.message.DaggerMessageComponent;
import com.yiqi.liebang.feature.news.dependencies.message.MessagePresenterModule;
import com.yiqi.liebang.feature.news.view.adapter.SysMsgAdapter;
import com.yiqi.liebang.feature.news.view.adapter.VistorAdapter;
import com.yiqi.liebang.feature.order.view.OrderInfoActivity;
import com.yiqi.liebang.feature.wallet.view.WalletActivity;
import com.yiqi.liebang.framework.DataServer;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class SysPushFragment extends BaseFragment implements MessageContract.View, BaseQuickAdapter.RequestLoadMoreListener, OnRefreshListener, BaseQuickAdapter.OnItemClickListener {


    @BindView(R.id.rv_sys)
    RecyclerView mRvSys;
    @BindView(R.id.smartRefesh)
    SmartRefreshLayout mSmartRefesh;

    private SysMsgAdapter mAdapter;
    private EmptyView mEmptyView;
    private int pageIndex = 1;
    private int pageSize = 10;
    List<MessageBo> mData;

    @Inject

    MessageContract.Presenter mPresenter;

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_sys_push;
    }

    @Override
    protected void initInjector() {
        DaggerMessageComponent.builder().messagePresenterModule(new MessagePresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initData() {
        initAdapter();
        if (!AM.user().isLogin()) {
            startActivity(new Intent(getActivity(), LoginActivity.class));
        } else {
            pageIndex = 1;
            mPresenter.getMessageList(pageIndex, pageSize);
        }

    }

    @Override
    protected void initEvent() {

    }


    private void initAdapter() {
        mSmartRefesh.setEnableLoadMore(false);
        mSmartRefesh.setOnRefreshListener(this);
        mEmptyView = new EmptyView(mRvSys);
        mRvSys.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvSys.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(2));
        mAdapter = new SysMsgAdapter();
        mAdapter.bindToRecyclerView(mRvSys);
        mAdapter.setOnLoadMoreListener(this, mRvSys);
        mAdapter.setOnItemClickListener(this);
    }

    @Override
    public void showListSuccess(List<MessageBo> workBoPageData) {
        mSmartRefesh.finishRefresh();
        mData = workBoPageData;
        setData(true, mData);
        mAdapter.setEnableLoadMore(true);
    }

    @Override
    public void showMessageListError(String error) {
        if (mSmartRefesh != null) mSmartRefesh.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getErrorView(error));
        mAdapter.setNewData(null);
        mAdapter.setEnableLoadMore(true);
        mSmartRefesh.finishRefresh();
    }

    @Override
    public void showMessageListMoreSuccess(List<MessageBo> msg) {
        setData(false, msg);
    }

    @Override
    public void showMessageListMoreError(String msg) {
        mAdapter.loadMoreFail();
    }

    @Override
    public void showMessageListEmpty() {
        if (mSmartRefesh != null) mSmartRefesh.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getEmptyView());
        mAdapter.setNewData(null);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refresh();
    }


    @Override
    public void onLoadMoreRequested() {
        loadMore();
    }


    private void refresh() {
        mAdapter.setEnableLoadMore(false);//这里的作用是防止下拉刷新的时候还可以上拉加载
        initData();
    }

    private void loadMore() {
        mPresenter.getMessageListMore(pageIndex, pageSize);
    }

    private void setData(boolean isRefresh, List data) {
        pageIndex++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdapter.setNewData(data);
        } else {
            if (size > 0) {
                mAdapter.addData(data);
            }
        }
        if (size < pageSize) {
            //第一页如果不够一页就不显示没有更多数据布局
            mAdapter.loadMoreEnd(isRefresh);
        } else {
            mAdapter.loadMoreComplete();
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        int type = mAdapter.getData().get(position).getPushType();
        int orderType = mAdapter.getData().get(position).getOrderType();
        Intent intent = null;
        switch (type) {

            case 0://订单
                switch (orderType) {
                    case 0://问答
                        intent = new Intent(getActivity(), AskInfoActivity.class);
                        intent.putExtra("order_id", mData.get(position).getOrderId());
                        intent.putExtra("is_expert", mData.get(position).getType() == 4 ? true : false);

                        break;
                    case 2://话题
                        intent = new Intent(getActivity(), OrderInfoActivity.class);
                        intent.putExtra("id", mData.get(position).getOrderId());
                        intent.putExtra("is_expert", mData.get(position).getType() == 4 ? true : false);

                        break;
                }
                break;

            case 15://基础认证审核通过
                intent = new Intent(getActivity(), CertificationSuccessActivity.class);
                intent.putExtra("type", Certificate.BASIC.getIndex());
                break;
            case 16:
                intent = new Intent(getActivity(), CertificationFailActivity.class);
                intent.putExtra("type", Certificate.BASIC.getIndex());
                break;
            case 17:
                intent = new Intent(getActivity(), CertificationSuccessActivity.class);
                intent.putExtra("type", Certificate.JAOYU.getIndex());
                break;
            case 18:
                intent = new Intent(getActivity(), CertificationFailActivity.class);
                intent.putExtra("type", Certificate.JAOYU.getIndex());
                break;
            case 19:
                intent = new Intent(getActivity(), CertificationSuccessActivity.class);
                intent.putExtra("type", Certificate.ZHIYE.getIndex());
                break;
            case 20:
                intent = new Intent(getActivity(), CertificationFailActivity.class);
                intent.putExtra("type", Certificate.ZHIYE.getIndex());
                break;
            case 21:

            case 22:

            case 23:
                intent = new Intent(getActivity(), CertificationSuccessActivity.class);
                intent.putExtra("type", Certificate.JIGOU.getIndex());
                break;
            case 24:
                intent = new Intent(getActivity(), CertificationFailActivity.class);
                intent.putExtra("type", Certificate.JIGOU.getIndex());
                break;
            case 50:
                intent = new Intent(getActivity(), WalletActivity.class);
                break;
            case 70:
                intent = new Intent(getActivity(), PendingIssuesActivity.class);
                break;


        }
        if (intent != null) {
            startActivity(intent);
        }
    }
}
